const db = require("../../db/sql");
const knex = require("../../db/knex");

async function selectQuestionnaire(res) {
    try{
        let onlyQuestionnaireInfo
        await knex.transaction(async (trx) => {
             onlyQuestionnaireInfo = await trx.select('*').from('save_questionnaire')

            for (const o of Array.from(onlyQuestionnaireInfo)) {
                const i = Array.from(onlyQuestionnaireInfo).indexOf(o);
                o.questionList = await trx.select('*').from('question_list').where({questionnaire_id: o.id})
                let questionnaire_id = o.id//对应问卷id
                for (const item of Array.from(o.questionList)) {
                    // const index = Array.from(o.questionList).indexOf(item);
                   let optionList = await trx('option_list').select('*').where({question_id: item.question_id,questionnaire_id:item.questionnaire_id})
                    item.optionList = optionList
                    // console.log(optionList,'选项列表和')
                }
                // console.log(o.questionList,"问题列表",o,"对应问卷","---------------")
            }
        })


        // console.log(Array.from(onlyQuestionnaireInfo.questionList))

        res.send({
            QuestionnaireInfo:onlyQuestionnaireInfo
        })
    }catch (e) {
        console.log(e)
    }finally {
   // await knex.destroy()
    }

}
module.exports = selectQuestionnaire
